package de.laures.cewolf.style;

import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;

import org.jfree.chart.StandardChartTheme;

import de.laures.cewolf.util.PropReader;

public class CewolfTheme {
	public static final CewolfTheme instance = new CewolfTheme();

	private StandardChartTheme chartTheme;

	private CewolfTheme() {
		//清晰化字体
		final boolean fontShape = PropReader.instance.getBoolean(StyleConstants.FONT_SHARP);
		chartTheme = new StandardChartTheme("default") {
			private static final long serialVersionUID = 6057496833883240860L;

			public void apply(org.jfree.chart.JFreeChart chart) {
				if (fontShape) {
					chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
							RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
				}
				super.apply(chart);
			};

		};

		chartTheme.setDrawingSupplier(new CustomDrawSupplier());
		initFont();
		initChart();
		initPlot();
	}

	private void initPlot() {
		Color plotBackgroundColor = PropReader.instance
				.getColor(StyleConstants.PLOT_BACKGROUND_COLOR);
		chartTheme.setPlotBackgroundPaint(plotBackgroundColor);

		Color rangeGridColor = PropReader.instance.getColor(StyleConstants.GRID_RANGE_COLOR);
		chartTheme.setRangeGridlinePaint(rangeGridColor);

		Color domainGridColor = PropReader.instance.getColor(StyleConstants.GRID_DOMAIN_COLOR);
		chartTheme.setDomainGridlinePaint(domainGridColor);

	}

	private void initChart() {
		Color chartBackgroundColor = PropReader.instance
				.getColor(StyleConstants.CHART_BACKGROUND_COLOR);
		chartTheme.setChartBackgroundPaint(chartBackgroundColor);
	}

	private void initFont() {
		chartTheme.setExtraLargeFont(getFont(StyleConstants.FONT_EXTRA_LARGE));
		chartTheme.setLargeFont(getFont(StyleConstants.FONT_LARGE));
		chartTheme.setRegularFont(getFont(StyleConstants.FONT_REGULAR));
		chartTheme.setSmallFont(getFont(StyleConstants.FONT_SMALL));
	}

	public StandardChartTheme getChartTheme() {
		return chartTheme;
	}

	private Font getFont(String name) {
		String fontName = PropReader.instance.getString(name + ".name");
		int style = PropReader.instance.getInteger(name + ".style");
		int size = PropReader.instance.getInteger(name + ".size");
		return new Font(fontName, style, size);
	}

}
